﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;

namespace MagicCard
{
    public partial class frmBaseMgmt : Form
    {
        private string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MagicCard.accdb";
        private OleDbConnection conn = null;
        private AccessDao accessDao = null;

        public frmBaseMgmt()
        {
            InitializeComponent();
        }

        private void frmBaseMgmt_Load(object sender, EventArgs e)
        {
            conn = new OleDbConnection(connStr);
            accessDao = new AccessDao(conn);
            Init();
        }

        private void Init()
        {
            this.cboStars.DataSource = accessDao.GetStarsFromCategory();
            this.cboStars.DisplayMember = "Stars";

            this.GetCategorys();

            this.GetBaseByCategory(this.cboCategory.Text.Trim());
        }

        private void GetCategorys()
        {
            this.cboCategory.DataSource = accessDao.GetCategoryByStars(this.cboStars.Text.Trim());
            this.cboCategory.DisplayMember = "CategoryName";
            this.cboCategory.ValueMember = "ID";
        }

        private void GetBaseByCategory(string categoryName)
        {
            this.grdBaseView.DataSource = accessDao.GetBaseCardByCategory(categoryName);
            this.grdBaseView.AutoGenerateColumns = false;
        }

        private void cboStars_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetCategorys();
        }

        private void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GetBaseByCategory(cboCategory.Text.Trim());
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.cboCategory.SelectedValue == null)
            {
                MessageBox.Show("请先选择一套卡名称！", "警告");
                return;
            }
            else
            {
                frmBaseCardAdd baseCardAdd = new frmBaseCardAdd(this.cboCategory.SelectedValue.ToString());
                DialogResult result = baseCardAdd.ShowDialog();
                if (result == DialogResult.Cancel)
                {
                    this.GetBaseByCategory(this.cboCategory.Text.Trim());
                }
            }
        }
    }
}
